package org.dromara.erp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.erp.domain.ErpUnitAttr;
import org.dromara.erp.mapper.ErpUnitAttrMapper;
import org.dromara.erp.service.IErpUnitAttrService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 多计量单位Service业务层处理
 *
 * @author Mr Lei
 * @date 2024-06-02
 */
@RequiredArgsConstructor
@Service
public class ErpUnitAttrServiceImpl implements IErpUnitAttrService {

    private final ErpUnitAttrMapper baseMapper;

    /**
     * 查询多计量单位
     */
    @Override
    public ErpUnitAttr queryById(Long id){
        return baseMapper.findById(id);
    }

    /**
     * 查询多计量单位列表
     */
    @Override
    public TableDataInfo<ErpUnitAttr> queryPageList(ErpUnitAttr bo, PageQuery pageQuery) {
        Page<ErpUnitAttr> result = baseMapper.queryPageList(pageQuery.build(), bo);
        return TableDataInfo.build(result);
    }

    /**
     * 查询多计量单位列表
     */
    @Override
    public List<ErpUnitAttr> queryList(ErpUnitAttr bo) {
        LambdaQueryWrapper<ErpUnitAttr> lqw = buildQueryWrapper(bo);
        return baseMapper.selectList(lqw);
    }

    private LambdaQueryWrapper<ErpUnitAttr> buildQueryWrapper(ErpUnitAttr bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ErpUnitAttr> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), ErpUnitAttr::getName, bo.getName());
        lqw.eq(bo.getProductId() != null, ErpUnitAttr::getProductId, bo.getProductId());
        lqw.eq(bo.getUnitId() != null, ErpUnitAttr::getUnitId, bo.getUnitId());
        return lqw;
    }

    /**
     * 新增多计量单位
     */
    @Override
    public Boolean insert(ErpUnitAttr bo) {
        validEntityBeforeSave(bo);
        return baseMapper.insert(bo) > 0;
    }

    /**
     * 修改多计量单位
     */
    @Override
    public Boolean update(ErpUnitAttr bo) {
        validEntityBeforeSave(bo);
        return baseMapper.updateById(bo) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ErpUnitAttr entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除多计量单位
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }


}
